using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame.Modes
{
    [System.Serializable]
    public class KFKeyEventConfigMode :KFMode
    {

        public static KFKeyEventConfigMode getModes()
        {
                if (!editObject)  
               editObject = new GameObject("KF_Config_edit");
            var mode =  editObject.AddComponent<KFKeyEventConfigMode>();
            return mode;
        }

        public string filename = "kfkeyslisent.ini";


        /// <summary>
        /// 所有事件
        /// </summary>
        /// <typeparam name="KFKeyEventMode"></typeparam>
        /// <returns></returns>
        [SerializeField]
         [Tooltip("键盘模拟记录集")]
        public List<KFKeyEventMode> eventModes = new List<KFKeyEventMode>();

        public override string FileName()
        {
            return filename;
        }

        public override void save()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            iNIParser.clean();

            for (var i = 0; i < eventModes.Count; i++)
            {
                if (eventModes[i].modeName == null)
                    eventModes[i].modeName = eventModes[i].name + i;

                iNIParser.WriteValue(eventModes[i].modeName, "enter", eventModes[i].enter);
                iNIParser.WriteValue(eventModes[i].modeName, "name", eventModes[i].name);
                iNIParser.WriteValue(eventModes[i].modeName, "id", eventModes[i].id);
                iNIParser.WriteValue(eventModes[i].modeName, "eventName", eventModes[i].eventName);
                iNIParser.WriteValue(eventModes[i].modeName, "targettype", eventModes[i].targettype);
                iNIParser.WriteValue(eventModes[i].modeName, "keycode", eventModes[i].KeyCode);
                iNIParser.WriteValue(eventModes[i].modeName, "keytype", eventModes[i].keytype);
                iNIParser.WriteValue(eventModes[i].modeName, "valueInt", eventModes[i].valueInt);
                iNIParser.WriteValue(eventModes[i].modeName, "valueString", eventModes[i].valueString);
                iNIParser.WriteValue(eventModes[i].modeName, "valuefloat", eventModes[i].valuefloat);
                iNIParser.WriteValue(eventModes[i].modeName, "des", eventModes[i].des);


            }
            iNIParser.save();

        }

        public override void LoadDatas()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            foreach (var item in iNIParser.SectionNames())
            {
                KFKeyEventMode mode = new KFKeyEventMode();
                mode.modeName = item;

                mode.enter = iNIParser.ReadValue(item, "enter", true);
                mode.name = iNIParser.ReadValue(item, "name", "");
                mode.id = iNIParser.ReadValue(item, "id", 1);
                mode.eventName = iNIParser.ReadValue(item, "eventName", "");
                mode.targettype = iNIParser.ReadValue(item, "targettype", "");
                mode.KeyCode = iNIParser.ReadValue(item, "keycode", "");
                mode.keytype = iNIParser.ReadValue(item, "keytype", 1);
                mode.valueInt = iNIParser.ReadValue(item, "valueInt", 1);
                mode.valueString = iNIParser.ReadValue(item, "valueString", "");
                mode.valuefloat = iNIParser.ReadValue(item, "valuefloat", 0);
                mode.des = iNIParser.ReadValue(item, "des", "");
                eventModes.Add(mode);

            }
        }
    }
}